import 'package:epidemic_data/config/palette.dart';
import 'package:epidemic_data/data/data.dart';
import 'package:epidemic_data/http/http_util.dart';
import 'package:epidemic_data/screens/news_detail_screen.dart';
import 'package:epidemic_data/widgets/custom_app_bar.dart';
import 'package:flutter/material.dart';
import 'package:epidemic_data/model/ncov_response.dart';

class NewsScreen extends StatefulWidget {
  @override
  _NewsScreenState createState() {
    return _NewsScreenState();
  }
}

class _NewsScreenState extends State<NewsScreen> {
  List<News> newsList = [];

  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Palette.primaryColor,
      appBar: CustomAppBar(),
      body: Padding(
        padding: EdgeInsets.all(8.0),
        child: Container(
          child: ListView.builder(
            itemCount: newsList.length,
            itemBuilder: (context, index) {
              return InkWell(
                onTap: () {
                  Navigator.push(context, MaterialPageRoute(
                      builder :(context) => NewsDetailsScreen(newsList[index])
                  ));
                },
                child: _newListWidget(newsList[index]),
              );
            },
          ),
        ),
      ),
    );
  }

  //获取网络数据
  void getData() {
    // HttpUtil.get(
    //     "http://api.tianapi.com/ncov/index",
    //     (data) {
    //       NcovResponse response = NcovResponse.fromJson(data);
    //       setState(() {
    //         newsList = response.newslist[0].news;
    //       });
    //     },
    //     params: {"key": "132133af3d8bdfb0c50d265159e15983"},
    //     errorCallBack: (errorMsg) {
    //       print("error:" + errorMsg);
    //     });

          NcovResponse response = NcovResponse.fromJson(nationalInfoFake);
          setState(() {
            newsList = response.newslist[0].news;
          });
  }

  Widget _newListWidget(News item) {
    return Card(
      elevation: 2.0,
      margin: EdgeInsets.only(bottom: 20.0),
      child: Padding(
        padding: EdgeInsets.all(8.0),
        child: Row(
          children: [
            Container(
              width: 80.0,
              height: 80.0,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/images/news.png'),
                  fit: BoxFit.cover,
                ),
                borderRadius: BorderRadius.circular(8.0),
              ),
            ),
            SizedBox(
              width: 5.0,
            ),
            Expanded(
                child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  item.title,
                  style: TextStyle(
                    fontSize: 18.0,
                  ),
                ),
                SizedBox(
                  height: 5.0,
                ),
                Row(
                  children: [
                    Icon(Icons.person),
                    Text(
                      item.infoSource,
                      style: TextStyle(
                        fontSize: 12.0,
                      ),
                    ),
                    SizedBox(
                      width: 10.0,
                    ),
                    Icon(Icons.date_range),
                    Text(
                      item.pubDateStr,
                      style: TextStyle(
                        fontSize: 12.0,
                      ),
                    )
                  ],
                )
              ],
            ))
          ],
        ),
      ),
    );
  }
}
